﻿using System;
using System.Linq;
using Coolite.Ext.Web;
using System.Xml.Xsl;
using System.Xml;
using System.Timers;

namespace GUESTSMANAGEMENT.View.Report
{
    public partial class gridPresence : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {

            EnventDateSelected(sender, e);
            /*this.reportAnaStore.DataSource = this.TestData;
            this.reportAnaStore.DataBind();
             * */

        }

        protected void Store1_RecordUpdated(object sender, AfterRecordUpdatedEventArgs e)
        {
            // This event is fired once for each Record that is Updated.
            /*
            var company = new
            {
                Name = e.NewValues["company"],
                Price = e.NewValues["price"],
                LastChange = e.NewValues["lastChange"]
            };

            string tpl = "Name: {0 }, Price: {1 }, LastChange: {2}<br />";
            this.Label1.Html += string.Format(tpl, company.Name, company.Price, company.LastChange);
             * */
        }


        protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            //string format = this.FormatType.Value.ToString();

            XmlNode xml = e.Xml;

            this.Response.Clear();

            this.Response.ContentType = "application/vnd.ms-excel";
            this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
            XslCompiledTransform xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("Content/Xls/Excel.xsl"));
            xtExcel.Transform(xml, null, Response.OutputStream);

            this.Response.End();
            //this.
        }



        protected void EnventDateSelected(object sender, EventArgs e)
        {
            //DateTime data = DateTime.Now;
            //Ext.Msg.Alert("Hora", "A hora é:" + data);
            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                try
                {
                    DateTime date = this.eventDate.SelectedDate;
                    gmevent evento = Controller.Event.getByEventDate(date);
                    int cduser = Convert.ToInt32(Session["userCd"]);
                    String nmuser = Session["userName"].ToString();
                    int fguser = Convert.ToInt32(Session["userType"]);
                    
                    if (evento != null)
                    {
                        // int cdUser = Convert.ToInt32(Session["userCd"]);
                        DateTime date2 = new DateTime(evento.DTEVENT.Value.Year, evento.DTEVENT.Value.Month, evento.DTEVENT.Value.Day + 1, evento.TMEXPIRE.Value.Hours, evento.TMEXPIRE.Value.Minutes, evento.TMEXPIRE.Value.Seconds);
                        var selectListGuests = from guestlists in context.gmlistguests.Include("gmlistguests.cdlistguests")
                                               from gevent in context.gmevent.Include("gmevent.nmevent")
                                               from user in context.gmuser.Include("gmuser.nmuser")
                                               where guestlists.GMUSER_CDUSER == user.CDUSER && gevent.CDEVENT == guestlists.GMEVENT_CDEVENT
                                               && gevent.DTEVENT == date && (user.CDUSER == cduser || cduser == -1)
                                               select new
                                               {
                                                   user.NMUSER,
                                                   guestlists.NMGUEST,
                                                   guestlists.NMMAILGUEST,
                                                   TMCONFIRM = (guestlists.TMCONFIRM <= date2 ? "Válido" : (guestlists.TMCONFIRM > date2) ? "Presente" : "Inválido"),
                                                   FGTYPEINVITE = (guestlists.FGTYPEINVITE == 1 ? "Bônus FEM" : guestlists.FGTYPEINVITE == 2 ? "Bônus MASC" : guestlists.FGTYPEINVITE == 3?"Free FEM":"Free MASC"),
                                                   gevent.NMEVENT,
                                                   gevent.DTEVENT
                                               };

                        var lista = selectListGuests.ToList();

                        this.reportPresenceStore.DataSource = lista;
                        this.reportPresenceStore.DataBind();

                        this.eventName.Text = evento.NMEVENT;
                        this.reportPresenceGridPanel.Enabled = true;

                        this.comboUser.SelectedItem.Value = nmuser;
                        this.comboUser.SelectedItem.Text = nmuser;
                        if (fguser == 1)
                        {
                            this.comboUser.Enabled = true;
                            this.userStore.DataSource = Controller.User.getGridListReport();
                            this.userStore.DataBind();
                        }
                    }
                    else
                    {
                        Ext.Msg.Alert("Alerta!", "Não há eventos cadastros nesta data!").Show();
                        this.reportPresenceGridPanel.Reload();
                    }
                }
                catch
                {
                    Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
                }
            }
        }

        public void UserSelected(object sender, AjaxEventArgs e)
        {
            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                DateTime date = this.eventDate.SelectedDate;
                gmevent evento = Controller.Event.getByEventDate(date);
                int cduser = Convert.ToInt32(this.comboUser.SelectedItem.Value.ToString());

                if (evento != null)
                {
                DateTime date2 = new DateTime(evento.DTEVENT.Value.Year, evento.DTEVENT.Value.Month, evento.DTEVENT.Value.Day + 1, evento.TMEXPIRE.Value.Hours, evento.TMEXPIRE.Value.Minutes, evento.TMEXPIRE.Value.Seconds);
                var selectListGuests = from guestlists in context.gmlistguests.Include("gmlistguests.cdlistguests")
                                       from gevent in context.gmevent.Include("gmevent.nmevent")
                                       from user in context.gmuser.Include("gmuser.nmuser")
                                       where guestlists.GMUSER_CDUSER == user.CDUSER && gevent.CDEVENT == guestlists.GMEVENT_CDEVENT
                                       && gevent.DTEVENT == date && (user.CDUSER == cduser || cduser == -1)
                                       select new
                                       {
                                           user.NMUSER,
                                           guestlists.NMGUEST,
                                           guestlists.NMMAILGUEST,
                                           TMCONFIRM = (guestlists.TMCONFIRM <= date2 ? "Válido" : (guestlists.TMCONFIRM > date2) ? "Presente" : "Inválido"),
                                           FGTYPEINVITE = (guestlists.FGTYPEINVITE == 1 ? "Bônus FEM" : guestlists.FGTYPEINVITE == 2 ? "Bônus MASC" : guestlists.FGTYPEINVITE == 3 ? "Free FEM" : "Free MASC"),
                                           gevent.NMEVENT,
                                           gevent.DTEVENT
                                       };

                var lista = selectListGuests.ToList();

                this.reportPresenceStore.DataSource = lista;
                this.reportPresenceStore.DataBind();
                }
                    else
                    {
                        Ext.Msg.Alert("Alerta!", "Não há eventos cadastros nesta data!").Show();
                        this.reportPresenceGridPanel.Reload();
                    }
            }
        }

        public String compare(DateTime tmconfirm, DateTime dateevent, TimeSpan time)
        {
            return "";
        }

        protected void filterDate(object sender, AjaxEventArgs e)
        {
        }
    }
}